package com.ete.service.service.location

import android.annotation.SuppressLint
import android.os.Build
import com.ete.service.entry.location.LocationEntry
import com.ete.service.entry.location.LocationResult
import com.ete.service.entry.location.LocationType
import com.ete.service.manager.ManagerConstant
import com.ete.service.manager.location.LocationManager
import com.ete.service.service.location.android.AndroidApiLocation
import com.ete.service.service.location.geo.GeoLocation
import com.ete.service.service.location.serial.SerialLocation
import com.ete.service.util.log.EteLog

/**
 * Author:LiXian
 * <p>
 * Desc:定位
 * Version:1.0
 * date 2025/4/7 17:46
 */
@SuppressLint("StaticFieldLeak")
object LocationUtil {

    private var mListener: ILocationListener? = null

    //拥有GPS模块（串口）的设备列表
    private val mGpsDevices: List<String> = listOf("LK-HT930", "LK-HT950")

    /**
     * 初始化定位
     *
     * 注：设备优先判断是否有GPS串口模块
     */
    @JvmStatic
    fun initLocation(mEntry: LocationEntry) {
        EteLog.d(LocationManager.TAG, "初始化定位mEntry:[$mEntry]")
        mListener = if (mGpsDevices.contains(mEntry.deviceMode)) {
            SerialLocation()
        } else {
            when (mEntry.locationType) {
                LocationType.TYPE_GEO -> GeoLocation()
                LocationType.TYPE_ANDROID_API -> AndroidApiLocation()
            }
        }
        mListener?.initLocation(mEntry)
    }

    /**
     * 检查权限
     */
    @JvmStatic
    fun checkedPermission(mCallback: (Int) -> Unit) {
        EteLog.d(LocationManager.TAG, "检查权限")
        mListener?.checkedPermission(mCallback) ?: ManagerConstant.CODE_NOT_INIT
    }

    /**
     * 开始定位
     */
    @JvmStatic
    fun startLocation(): Int {
        EteLog.d(LocationManager.TAG, "开始定位，检查定位是否可用")
        return mListener?.startLocation() ?: ManagerConstant.CODE_NOT_INIT
    }

    /**
     * 停止定位
     */
    @JvmStatic
    fun stopLocation() {
        EteLog.d(LocationManager.TAG, "停止定位")
        mListener?.stopLocation()
    }

    /**
     * 销毁定位
     */
    @JvmStatic
    fun destroyLocation() {
        EteLog.d(LocationManager.TAG, "销毁定位")
        mListener?.destroyLocation()
        mListener = null
    }

    /**
     * 跳转打开GSP定位
     */
    @JvmStatic
    fun navigationOpenGps() {
        EteLog.d(LocationManager.TAG, "跳转打开GSP定位")
        mListener?.navigationOpenGps()
    }

    @JvmStatic
    fun getErrorByCode(code: Int): String {
        return ManagerConstant.ERROR_CODE.get(code) ?: "未知错误"
    }

    /**
     * 获取定位结果
     */
    @JvmStatic
    fun getLocationResult(): LocationResult {
        return mListener?.getLocationResult() ?: LocationResult()
    }

    /**
     * 是否串口（GPS）定位
     */
    @JvmStatic
    fun isSerialLocation(): Boolean {
        return mListener?.isSerialLocation() ?: false
    }

    /**
     * 是否正在定位
     */
    @JvmStatic
    fun isLocationning(): Boolean {
        return mListener?.isLocationning() ?: false
    }

    /**
     * 是否有GPS模块定位
     */
    @JvmStatic
    fun isGpsModel(): Boolean {
        return mListener?.isGpsModel() ?: false
    }
}